function kshaz(X,d)
% function kshaz(X)
% GUI for kernel estimates of cumulative distribution function
% X - random sample
%_
% (C) Jiri Zelinka, Jan Kolacek, Masaryk University, Brno, Czech Republic
%

addpath ./base
addpath ./functions


K=K_def('quart');

if nargin==0
 X=[];
 xx=[];
 d=[];
 h=[];
 haf_est=[];
 bounds=[];
elseif isfield(X,'X') % structure is on input
 ud=X;
 X=ud.X;
 d=ud.d;
 K=ud.K;
 minX=min(X);
 maxX=max(X);
 DX=maxX-minX;
 xx=linspace(minX-0.1*DX,maxX+0.1*DX,201);
 h=cvmin(X,d,K);
 haf_est=K_hafest(X,d,K,xx,h);
else % data X is on input
 minX=min(X);
 maxX=max(X);
 DX=maxX-minX;
 xx=linspace(minX-0.1*DX,maxX+0.1*DX,201);
 h=cvmin(X,d,K);
 haf_est=K_hafest(X,d,K,xx,h);
 bounds=[min(X),max(X)];
end

main_data=struct('K',K,'X',X,'d',d,'xx',xx,'h',h,'haf_est',haf_est);

CloseMainStr='set(gcf,''CloseRequestFcn'',''closereq'');delete(gcf);';


main_fig=figure( ...
   'Visible','on', ...
   'Name','Kernel HAZ Estimate', ...
   'CloseRequestFcn',CloseMainStr,...
   'Units','Normalized',...
   'NumberTitle','off',...
   'Tag','HAZ_MAIN');

set(main_fig,'UserData',main_data);


if isempty(X)

CloseStr='set(gcf,''CloseRequestFcn'',''closereq'');clear all';


datafig=figure( ...
   'Visible','on', ...
   'Name','Data Input', ...
   'CloseRequestFcn',CloseStr,...
   'Units','Normalized',...
   'Tag','HAZ_DATA',...
   'NumberTitle','off');

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.4,.85,.2,.1], ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',0.5,...
        'String','Data Input');

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.1,.75,.5,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',0.4,...
        'String','Select a file to load (the variables has to be named X and d):');

% The LAOD button
LoadStr='uiload;delete(gcf);mf=findobj(''Tag'',''HAZ_MAIN'');delete(mf);if exist(''X'',''var'') & exist(''d'',''var''),kshaz(X,d); else, kshaz; end';
%LoadStr='HAZ_pom2';
uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.6,.8,.2,0.07], ...
        'String','Load', ...
        'Callback',LoadStr);


% The Cancel button
CancelStr='mf=findobj(''Tag'',''HAZ_MAIN'');delete(gcf);delete(mf);';
uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.6,.5,.2,0.07], ...
        'String','Cancel', ...
        'Callback',CancelStr);


else % if isempty(X)


axes('Position',[0.08 0.08 0.7 0.85]);

plot(X,zeros(size(X)),'x');
tit=title('Data');
set(tit,'FontUnits','Normalized');
set(tit,'FontSize',0.05);


%axis([minX-0.1*DX,maxX+0.2*maxX,-1,1]);
    hh=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.85,0,.20,1], ...
        'BackgroundColor',[0.50 0.50 0.50]);

top=0.825;
%  text Data
    DataHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.085,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Data');
%  button Data input
    DataHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.861,top+0.05,.125,0.05], ...
        'String','Input', ...
        'Callback','HAZ_datainput');
%  button Data view
    DataHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','HAZ_datadraw');
%  button Data save
    DataHndl4=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.125,0.05], ...
        'String','Save', ...
        'Callback','HAZ_datasave');



top=0.6;
%  Empirical distribution function
    FempHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.085,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Kaplan-Meier');
    FempHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'String','View', ...
        'Callback','HAZ_KMdraw');
%    FempHndl2=uicontrol( ...
%        'Style','push', ...
%        'Units','normalized', ...
%        'FontUnits','normalized',...
%        'Position',[0.86,top,.125,0.05], ...
%        'String','View', ...
%        'Callback','HAZ_histdraw');

top=0.425;
%  Kernels
    KerHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.085,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Kernel');
    KerHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'String','Choice', ...
        'Callback','HAZ_kerchoice;');
    KerHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','HAZ_kerdraw;');
    KerHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.125,0.05], ...
        'String','Params', ...
        'Callback','HAZ_kerpars;');


top=0.2;
%  estimates
    EstHndl1=uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.085,.125,0.05], ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'String','Estimate');
    EstHndl3=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.05,.125,0.05], ...
        'String','Params', ...
        'Callback','HAZ_estimpars');
    EstHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top,.125,0.05], ...
        'String','View', ...
        'Callback','HAZ_estimdraw');
    EstHndl2=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top-0.05,.125,0.05], ...
        'String','Conf.int.', ...
        'Callback','HAZ_confint');



CloseStr='set(gcf,''CloseRequestFcn'',''closereq'');delete(gcf);';

top=0.01;
% button print
    labelStr='Print';
    call1='curr=get(gcf,''CurrentAxes'');fig=figure;cc=copyobj(curr,fig);';
    call2='set(cc,''Position'',[0.08 0.08 0.85 0.85]);';
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.001,.96,.067,0.035], ...
        'String',labelStr, ...
        'Callback',[call1,call2]);
    
    
top=0.005;
% button close
    labelStr='Close';
    CloseHndl=uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.86,top+0.025,.125,0.075], ...
        'String',labelStr, ...
        'Callback',CloseStr);
     
end % isempty(X)
